package com.lingshu.app.ui.report;

import androidx.lifecycle.MutableLiveData;

import com.lingshu.app.model.report.ReportAuditRequest;
import com.lingshu.app.model.report.ThyroidReportSaveRequest;
import com.lingshu.app.model.report.ThyroidReportSaveResponse;
import com.lingshu.app.model.structure.ThyroidStructureData;
import com.lingshu.app.network.CustomDisposable;
import com.lingshu.app.network.RetrofitMobileClient;
import com.lingshu.core.utils.KLog;
import com.lingshu.app.service.ReportService;

/**
 * 甲状腺智能报告 ViewModel
 */
public class FormThyroidFragmentViewModel extends CommonViewModel {
    public MutableLiveData<ThyroidStructureData> structureData = new MutableLiveData<>();
    public MutableLiveData<ThyroidReportSaveResponse> reportSaveResponse = new MutableLiveData<>();

    @Override
    public void initData() {
        if (structureData.getValue() == null) {
            structureData.postValue(new ThyroidStructureData());
        }
        super.initLiveData();
    }

    /**
     * 设置结论
     *
     * @param value
     * @param isRight
     */
    public void setStructureDataExceptional(Integer value, boolean isRight) {
        if (structureData.getValue() != null) {
            if (isRight) structureData.getValue().setExceptionalThyroidR(value);
            else structureData.getValue().setExceptionalThyroidL(value);
            if (isRight) {
                allowInputR.postValue(structureData.getValue().getEnableThyroidR());
            } else {
                allowInputL.postValue(structureData.getValue().getEnableThyroidL());
            }
        }
    }

    /**
     * 获取报告数据
     *
     */
    public void loadData() {
        CustomDisposable.addDisposable(RetrofitMobileClient.getInstance().getService(ReportService.class).getThyroidReport(registerInfo), returnObject -> {
            if (returnObject.isSuccess()) {
                setStructureData(returnObject.getData());
                structureData.postValue(returnObject.getData());
            }
            canSave.postValue(true);
        }, throwable -> {
            KLog.e(TAG, throwable.getMessage());
            infoMsg.postValue(throwable.getMessage());
        });
    }

    /**
     * 根据Exceptional值 改变Enable属性
     *
     * @param data
     */
    public void setStructureData(ThyroidStructureData data) {
        if (data.getExceptionalThyroidR() >= 2) {
            data.setEnableThyroidR(true);
        }
        if (data.getExceptionalThyroidL() >= 2) {
            data.setEnableThyroidL(true);
        }
    }

    /**
     * 保存报告
     */
    @Override
    public void saveReport() {
        if (registerInfo.getAllowSingleSide()) {
            if (screenshotResponse1 == null && screenshotResponse2 == null) {
                infoMsg.postValue("图像不满足条件");
                return;
            }
            if (structureData.getValue().getExceptionalThyroidR() == -2 && structureData.getValue().getExceptionalThyroidL() == -2) {
                infoMsg.postValue("双侧数据不能都是不合格的");
                return;
            }
        } else {
            if (screenshotResponse1 == null || screenshotResponse2 == null) {
                infoMsg.postValue("报告截图不全");
                return;
            }
            if (structureData.getValue().getExceptionalThyroidR() == -2 || structureData.getValue().getExceptionalThyroidL() == -2) {
                infoMsg.postValue("不允许生成单侧报告");
                return;
            }
        }
        ThyroidReportSaveRequest saveRequest = new ThyroidReportSaveRequest();
        saveRequest.setRegisterInfo(registerInfo);
        saveRequest.setScreenshotResponse1(screenshotResponse1);
        saveRequest.setScreenshotResponse2(screenshotResponse2);
        saveRequest.setThyroidForm(structureData.getValue());
        canSave.postValue(false);
        CustomDisposable.addDisposable(RetrofitMobileClient.getInstance().getService(ReportService.class).saveThyroidReport(saveRequest), returnObject -> {
            if (returnObject.isSuccess()) {
                reportSaveResponse.postValue(returnObject.getData());
                infoMsg.postValue("保存成功");
            } else {
                infoMsg.postValue(returnObject.getMessage());
            }
        });
    }

    /**
     * 审核报告
     */
    @Override
    public void auditReport() {
        ReportAuditRequest request = new ReportAuditRequest();
        request.setRegisterInfo(registerInfo);
        request.setReportId(structureData.getValue().getId());
        canSave.postValue(false);
        canAudit.postValue(false);
        CustomDisposable.addDisposable(RetrofitMobileClient.getInstance().getService(ReportService.class).auditReport(request), returnObject -> {
            if (returnObject.isSuccess()) {
                infoMsg.postValue("保存成功");
                reportAuditResponse.postValue(returnObject.getData());
            } else {
                infoMsg.postValue(returnObject.getMessage());
            }
        });
    }
}
